package com.gancp;

import com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter;
import com.gancp.common.SessionFilter;
import org.mybatis.spring.annotation.MapperScan;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.http.HttpMessageConverters;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.PropertySource;
import org.springframework.context.support.PropertySourcesPlaceholderConfigurer;
import org.springframework.http.converter.HttpMessageConverter;

import javax.servlet.Filter;

@SpringBootApplication
@MapperScan("com.gancp.dao")
@PropertySource(value = "classpath:parameter.properties")
public class DemoApplication {


    public static void main(String[] args) {
        SpringApplication.run( DemoApplication.class, args );
    }

    /**
     * @Author gancp
     * @Date 时间被黑洞吸走了
     * @Descripition fastjson消息转换器
     **/
    @Bean
    public HttpMessageConverters fastJsonHttpMessageConverter() {
        FastJsonHttpMessageConverter fastJsonHttpMessageConverter = new FastJsonHttpMessageConverter();
        HttpMessageConverter converter = fastJsonHttpMessageConverter;
        return new HttpMessageConverters( converter );
    }

    /**
     * 配置过滤器
     *
     * @return
     */
    @Bean
    public FilterRegistrationBean someFilterRegistration() {
        FilterRegistrationBean registration = new FilterRegistrationBean();
        registration.setFilter( sessionFilter() );
        registration.addUrlPatterns( "/*" );
        registration.addInitParameter( "paramName", "paramValue" );
        registration.setName( "sessionFilter" );
        return registration;
    }

    /**
     * 创建一个bean
     *
     * @return
     */
    @Bean(name = "sessionFilter")
    public Filter sessionFilter() {
        return new SessionFilter();
    }

    /**
     * @Author gancp
     * @Date 时间被黑洞吸走了
     * @Descripition 占位符解析配置文件
     **/
    @Bean
    public static PropertySourcesPlaceholderConfigurer placeholderConfigurer() {
        return new PropertySourcesPlaceholderConfigurer();
    }
}
